package com.android.porttallymansystem.logic.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.android.porttallymansystem.R
import com.android.porttallymansystem.logic.Repository
import com.android.porttallymansystem.logic.dao.UserDao
import com.android.porttallymansystem.logic.model.User
import kotlin.concurrent.thread

@Database(version = 2, entities = [User::class])
abstract class UserDatabase : RoomDatabase() {

    abstract fun userDao(): UserDao

    companion object {

        private var instance: UserDatabase? = null

        fun getDatabase(context: Context): UserDatabase {
            instance?.let {
                return it
            }
            return Room.databaseBuilder(
                context.applicationContext,
                UserDatabase::class.java,
                "user_database"
            ).build().apply {

                // 初始化用户的模拟数据
                thread {
                    val userList = mutableListOf<User>(
                        User(
                            "党奥",
                            "男",
                            21,
                            R.drawable.profile_dangao,
                            "123456",
                            "13277042698",
                            "2580021571@qq.com"
                        ),
                        User(
                            "余沐钒",
                            "男",
                            21,
                            R.drawable.profile_yumufan,
                            "123456",
                            "15773118198",
                            "787065266@qq.com"
                        ),
                        User(
                            "杨成彬",
                            "男",
                            22,
                            R.drawable.profile_yangchengbin,
                            "123456",
                            "17862689431",
                            "1783956203@qq.com"
                        ),
                        User(
                            "孙田丰",
                            "男",
                            21,
                            R.drawable.profile_suntianfeng,
                            "123456",
                            "15586259125",
                            "2812403455@qq.com"
                        ),
                        User(
                            "李剑波",
                            "男",
                            21,
                            R.drawable.profile_lijianbo,
                            "123456",
                            "17364013173",
                            "1958615812@qq.com"
                        ),
                        User(
                            "徐鑫宇",
                            "男",
                            21,
                            R.drawable.profile_xuxinyu,
                            "123456",
                            "15671965196",
                            "1913283948@qq.com"
                        )
                    )

                    val userDao = this.userDao()
                    for (user in userList) {
                        val dbUser = userDao.loadUserByPhone(user.phone)
                        if (dbUser == null) {
                            userDao.insertUser(user)
                        } else {
                            dbUser.profile = user.profile
                            userDao.updateUser(dbUser)
                        }
                    }
                }

                instance = this
            }
        }

    }
}